<?php 
include("util.php");
include("header.php");
?>
<link href="/css/home.css?v=1" rel="stylesheet" type="text/css"></link>
<!--[if lt IE 7]>
<style type="text/css">
div.icon-disc {
  display : none;
}
div.home-block ul {
  margin:6px 2px;
}
div.home-block li {
  margin-left:18px;
  line-height:21px;
}
</style>
<![endif]-->

<?php 
$conn = mysql_connect("127.0.0.1","root","");
mysql_select_db("xingtan");

if(intval($_GET['topic-preview']) > 0) {
  $sql=sprintf("SELECT title, picture, articles FROM topics ORDER BY id DESC limit 4");
} else {
  $sql=sprintf("SELECT title, picture, articles FROM topics WHERE state > 0 ORDER BY id DESC limit 4");
}

$topics = array();
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
  array_push($topics, $row);
}
?>

<div class="container">
<div class="home-main">
  <?php
    if (!($category_map = apc_fetch('category-map'))) {
      $category_map = array();
      $sql=sprintf("SELECT id, name, title FROM categories;");
      $result = mysql_query($sql);
      while($r = mysql_fetch_array($result)) {
        $category_map[$r['id']] = $r;
      }
      apc_add('category-map', $category_map, 3600);
    }
    $cats = array(0x700, 0x200, 0x600, 0x100, 0x300, 0x500, 0x400, 0x0);

    $i = 0;
    foreach($cats as $cat_id) {
      $float_side = $i % 2 > 0 ? 'right' : 'left';
      printf('<div class="home-block %s">', $float_side);
      printf('<div class="home-block-title"><a target="_blank" href="/cat%d/?f=hcat-name">%s</a> <a class="more" target="_blank" href="/cat%d/?f=hcat-more">更多</a></div>', 
          $cat_id, $category_map[$cat_id]['name'], $cat_id);        
      printf('<ul>');

      $sql=sprintf("SELECT id FROM articles WHERE category = %d AND level > 15 ORDER BY id DESC LIMIT 12;", $cat_id);
      $result = mysql_query($sql);
      $li_count = 0;
      $max_title_len = 19;
      while($row = mysql_fetch_array($result)) {
        $article_meta = GetArticleMeta($row['id']);
        if ($article_meta) {
          if (mb_strlen($article_meta['a_title'], 'utf-8') > $max_title_len) {
            printf('<div class="icon-disc"></div><li><a target="_blank" title="%s" href="/a/%d.html">%s</a></li>', 
                $article_meta['a_title'], $row['id'], TruncateStr($article_meta['a_title'], $max_title_len));
          } else {
            printf('<div class="icon-disc"></div><li><a target="_blank" href="/a/%d.html">%s</a></li>', 
                $row['id'], $article_meta['a_title']);
          }
          if ($li_count++ >= 9) {
            break;
          }
        }
      }

      printf('</ul>');
      printf('</div>');
      $i++;
    }
  ?>
</div>
<div id="home-side">
  <div class="home-block left" style="width:97%;">
    <div class="home-block-title"><span>精华推荐</span> <a class="more" target="_blank" href="/essence/1/">更多</a></div>
    <ul>
    <?php
        $sql=sprintf("SELECT id FROM articles WHERE category >= 0 AND level > 20 ORDER BY id DESC LIMIT 10;");
        $result = mysql_query($sql);
        while($row = mysql_fetch_array($result)) {
          $article_meta = GetArticleMeta($row['id']);
          if ($article_meta) {
            printf('<div class="icon-disc"></div><li><a target="_blank" href="/a/%d.html">%s</a></li>', 
                $row['id'], $article_meta['a_title']);
          }
       }
    ?>
    </ul>
  </div>
  
  <div class="home-block left" style="width:97%;">
    <div class="home-block-title"><span>最新文章</span> <a class="more" target="_blank" href="/page/1/">全部文章</a></div>
    <ul>
    <?php
        $sql=sprintf("SELECT id FROM articles WHERE category >= 0 AND level > 0 ORDER BY id DESC LIMIT 20;");
        $result = mysql_query($sql);
        while($row = mysql_fetch_array($result)) {
          $article_meta = GetArticleMeta($row['id']);
          if ($article_meta) {
            printf('<div class="icon-disc"></div><li><a target="_blank" href="/a/%d.html">%s</a></li>', 
                $row['id'], $article_meta['a_title']);
          }
          // array_push($topics, $row);
       }
    ?>
    </ul>
  </div>
</div>
</div>
<?php 
include("footer.php");
?>
